//
//  HMStatusOriginalView.swift
//  新浪微博
//
//  Created by 饶鹏伟 on 16/4/11.
//  Copyright © 2016年 饶鹏伟. All rights reserved.
//
//cell上面显示的原创微博的内容

import UIKit
import SnapKit
import SDWebImage

//头像距离cell的边距
let StatusCellMargin: CGFloat = 8
//原创微博的视图
class HMStatusOriginalView: UIView {
    
    
    //原创微博视图的底部约束
    var bottomCons: Constraint?


    var statusViewModel: HMStatusViewModel?{
        
        didSet{
            
           //绑定模型的数据
            //头像图片
            iconView.sd_setImageWithURL(statusViewModel?.headImageURL)
            //用户名
            nameLabel.text=statusViewModel?.status?.user?.name
            //用户等级
            mbRankImageView.image = statusViewModel?.mbImage
            //用户认证类型
            verifiedImageView.image = statusViewModel?.verifiedImage
            //微博正文
            contentLabel.text = statusViewModel?.status?.text
            //设置时间
            timeLabel.text=statusViewModel?.time
            //微博来源
            sourceLabel.text = statusViewModel?.source
            
            //给原创微博配图视图设置数据源
            //取消约束
            self.bottomCons?.uninstall()
            //判断是否有配图视图
            if let urls=statusViewModel?.status?.pic_urls where urls.count>0
            {
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                self.bottomCons=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                //设置数据源
                pictureView.imageURLs=urls
                //显示配图视图
                pictureView.hidden=false
                
            }else{
                
                
              //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                self.bottomCons=make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                pictureView.imageURLs=nil
                //隐藏配图视图
                pictureView.hidden=true
                
                
            }
        }

    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面的显示
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
   //设置UI
    
    private func setupUI(){
        //设置背景色
       backgroundColor = UIColor.whiteColor()
       //设置分割条的颜色
        sepView.backgroundColor=UIColor.redColor()
        //头像圆角的处理
        iconView.cornerRadius = 20
        //添加子控件
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置每个子控件的约束
        
        sepView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.top.equalTo(self)
            
            make.height.equalTo(StatusCellMargin)
        }
        
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
        //设置原创微博的底部相对微博正文的底部约束 
        self.snp_makeConstraints { (make) -> Void in
        self.bottomCons=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

        
    }
    
    
   //懒加载子控件
    
    //用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //用户等级
   private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    
    //用户名
    private lazy var nameLabel: UILabel = UILabel(text: "今天心情不错", textColor:UIColor.darkGrayColor(), fontSize: 14)
    ///微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "22:22", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有女朋友", textColor: UIColor.darkGrayColor(), fontSize: 15, margin: StatusCellMargin)
    
    //配图视图
    private lazy var pictureView: HMStatusPictureView=HMStatusPictureView()
    
    //分隔条
    private lazy var sepView: UIView = UIView()
    
    
}
